﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Controllers.Utility;

namespace WebHoiChoMuaBan.Controllers
{
    public class HomeController : PublicController
    {

        public ActionResult Login()
        {
            ViewData["MainMenu"] = new PageTypeHelper().getAllPageTypes();
            return View("~/Views/Home/Login.aspx");
        }

        public ActionResult Register()
        {
            return View("~/Views/Home/Register.aspx");
        }

        public ActionResult Index()
        {
            List<Member> hotMember;
            base.loadCommon();
            base.LoadMainSearch();
            NewsProductHelper hNPro = new NewsProductHelper();
            ViewData["NewProducts"] = hNPro.GetHotOrderNews(null,null,1, 8);
            NewsProductHelper.FetchProductResult lstPro = hNPro.getHotNews(null, null, 1, 5);
            try
            {
                Random rnd = new Random();
                lstPro.products = (from item in lstPro.products
                                   orderby rnd.Next()
                                   select item).ToList<NewsProduct>().GetRange(0, 5);
                base.ViewData["HotProducts"] = lstPro;
            }
            catch
            {
                base.ViewData["HotProducts"] = lstPro;
            }
            try
            {
                hotMember = hNPro.gethotMember();
                Random rnd = new Random();
                base.ViewData["HotMember"] = (from item in hotMember
                                              orderby rnd.Next()
                                              select item).ToList<Member>().GetRange(0, 6);
            }
            catch
            {
                hotMember = hNPro.gethotMember();
                base.ViewData["HotMember"] = hotMember;
            }
            return base.View("~/Views/Home/Index.aspx");
        }

        public ActionResult Page(string pageName)
        {
            int pageId;
            int cateId;
            int pageNum;
            int needSell;
            int provinceId;
            if (!int.TryParse(base.Request.QueryString["need"], out needSell))
            {
                needSell = 0;
            }
            if (!int.TryParse(base.Request.QueryString["prvid"], out provinceId))
            {
                provinceId = 1;
            }
            if ((int.TryParse(base.Request.QueryString["pid"], out pageId) && int.TryParse(base.Request.QueryString["cid"], out cateId)) && int.TryParse(base.Request.QueryString["p"], out pageNum))
            {
                if (pageId <= 0)
                {
                    return this.Index();
                }
                if (base.Request.QueryString.Get("cn") != null)
                {
                    needSell = 4;
                }
                base.loadCommon();
                base.LoadMainSearch();
                PageType page = new PageTypeHelper().getPageType(pageId);
                if (page != null)
                {
                    NewsProductHelper.FetchProductResult lstPro;
                    CategoryHelper hCate = new CategoryHelper();
                    List<Category> lstCate = hCate.getCates(new int?(pageId));
                    base.ViewData["Categories"] = lstCate;
                    NewsProductHelper hNPro = new NewsProductHelper();
                    if (cateId != 0)
                    {
                        base.ViewData["NewProducts"] = hNPro.GetHotOrderNews(new int?(pageId), new int?(cateId), 1, 8);
                        lstPro = hNPro.getHotNews(new int?(pageId), new int?(cateId), 1, 5);
                        try
                        {
                            Random rnd = new Random();
                            List<NewsProduct> qList = (from item in lstPro.products
                                                       orderby rnd.Next()
                                                       select item).ToList<NewsProduct>();
                            lstPro.products = qList.GetRange(0, 5);
                            base.ViewData["HotProducts"] = lstPro;
                        }
                        catch
                        {
                            base.ViewData["HotProducts"] = lstPro;
                        }
                    }
                    else
                    {
                        base.ViewData["NewProducts"] = hNPro.GetHotOrderNews(new int?(pageId), new int?(cateId), 1, 8);
                        lstPro = hNPro.getHotNews(new int?(pageId), new int?(cateId), 1, 5);
                        try
                        {
                            Random rnd = new Random();
                            lstPro.products = (from item in lstPro.products
                                               orderby rnd.Next()
                                               select item).ToList<NewsProduct>().GetRange(0, 5);
                            base.ViewData["HotProducts"] = lstPro;
                        }
                        catch
                        {
                            base.ViewData["HotProducts"] = lstPro;
                        }
                    }
                    base.ViewData["CurrentCategory"] = hCate.getCate(cateId);
                    base.ViewData["CurrentPage"] = page;
                    base.ViewData["NeedSell"] = needSell;
                    base.ViewData["ProvinceId"] = provinceId;
                    return base.View(string.Format("~/Views/Home/{0}_List.aspx", page.Template.TemFilePath));
                }
            }
            return this.Index();
        }

        public string AjaxSearch(string inputText, string catId)
        {
            string str = "";
            if (!catId.Equals("-1"))
            {
                List<NewsProduct> result = new NewsProductHelper().getProductByTitle(inputText, catId);
                foreach (NewsProduct pro in result)
                {
                    str += pro.NProTitle + "@" + pro.NProId + ",";
                }
                try
                {
                    str = str.Substring(0, str.Length - 1);
                }
                catch (Exception)
                {
                }
                return str;
            }
            List<Member> result1 = new MemberHelper().getAllMembers();
            foreach (Member r in result1)
            {

                str += r.MemUsername + "@" + r.MemId + ",";
            }
            return str;
        }


        public ActionResult SearchNews(string inputDate)
        {
            int pageId;
            // int cateId;
            int pageNum;
            int needSell;
            int provinceId;
            if (!int.TryParse(Request.QueryString["need"], out needSell))
                needSell = NewsProductHelper.NEED_SELL;
            if (!int.TryParse(Request.QueryString["prvid"], out provinceId))
                provinceId = 1;

            if (int.TryParse(Request.QueryString["pid"], out pageId) &&
                //   int.TryParse(Request.QueryString["cid"], out cateId) &&
               int.TryParse(Request.QueryString["p"], out pageNum))
            {
                if (pageId <= 0)
                    return Index();
                loadCommon();
                PageTypeHelper hPage = new PageTypeHelper();
                PageType page = hPage.getPageType(pageId);
                if (page != null)
                {
                    CategoryHelper hCate = new CategoryHelper();
                    List<Category> lstCate = hCate.getCates(pageId);
                    ViewData["Categories"] = lstCate;
                    NewsProductHelper hNPro = new NewsProductHelper();
                    ViewData["NewProducts"] = hNPro.AllNewsBySearch(pageNum, Config.ITEMS_PER_PAGE, null, Request.QueryString["date"].ToString());

                    var lstPro = hNPro.getHotNews(null, null, 1, 5);
                    ViewData["HotProducts"] = lstPro;


                    ViewData["CurrentCategory"] = hCate.getCate(0);
                    ViewData["CurrentPage"] = page;
                    ViewData["NeedSell"] = needSell;
                    ViewData["ProvinceId"] = provinceId;
                    //}
                }
                return View(string.Format("~/Views/Home/{0}_List.aspx", page.Template.TemFilePath));
            }
            return Index();
        }

        public ActionResult ViewSaved()
        {
            int pageId = 10;
            int pageNum;
            int needSell;
            int provinceId;
            if (!int.TryParse(Request.QueryString["need"], out needSell))
                needSell = NewsProductHelper.NEED_SELL;
            if (!int.TryParse(Request.QueryString["prvid"], out provinceId))
                provinceId = 1;

            if (
               int.TryParse(Request.QueryString["p"], out pageNum))
            {
                loadCommon();
                PageTypeHelper hPage = new PageTypeHelper();
                PageType page = hPage.getPageType(pageId);
                if (page != null)
                {
                    CategoryHelper hCate = new CategoryHelper();
                    List<Category> lstCate = hCate.getCates(10);
                    ViewData["Categories"] = lstCate;
                    List<int> list = new List<int>();
                    if (Session["MemId"] == null)
                    {
                        string savedNews = Session["MemberSavedNewsProduct"] + "";
                        string[] newsid = savedNews.Split('@');
                        for (int i = 0; i < newsid.Length; i++)
                        {
                            if (!newsid[i].Equals(""))
                                list.Add(int.Parse(newsid[i]));
                        }
                    }
                    else
                    {
                        List<MemberSaveNewsProduct> proList = new NewsProductHelper().getAllByMemId(int.Parse(Session["MemId"] + ""));
                        foreach (MemberSaveNewsProduct tmp in proList)
                        {
                            list.Add(tmp.NewsProductId);
                        }
                    }

                    NewsProductHelper hNPro = new NewsProductHelper();
                    ViewData["NewProducts"] = hNPro.AllNewsProductSaved(pageNum, Config.ITEMS_PER_PAGE, null, list);
                    var lstPro = hNPro.getHotNews(null,null,1, 5);
                    ViewData["HotProducts"] = lstPro;
                    ViewData["CurrentCategory"] = hCate.getCate(0);
                    ViewData["CurrentPage"] = page;
                    ViewData["NeedSell"] = needSell;
                    ViewData["ProvinceId"] = provinceId;
                    //}
                }
                return View(string.Format("~/Views/Home/SearchResult.aspx"));
            }
            return Index();
        }



        public ActionResult Search(string inputText)
        {
            int pageId;
            // int cateId;
            int pageNum;
            int needSell;
            int provinceId;
            if (!int.TryParse(Request.QueryString["need"], out needSell))
                needSell = NewsProductHelper.NEED_SELL;
            if (!int.TryParse(Request.QueryString["prvid"], out provinceId))
                provinceId = 1;

            if (int.TryParse(Request.QueryString["pid"], out pageId) &&
                //   int.TryParse(Request.QueryString["cid"], out cateId) &&
               int.TryParse(Request.QueryString["p"], out pageNum))
            {
                if (pageId <= 0)
                    return Index();
                loadCommon();
                PageTypeHelper hPage = new PageTypeHelper();
                PageType page = hPage.getPageType(pageId);
                if (page != null)
                {
                    CategoryHelper hCate = new CategoryHelper();
                    List<Category> lstCate = hCate.getCates(pageId);
                    ViewData["Categories"] = lstCate;
                    NewsProductHelper hNPro = new NewsProductHelper();
                    ViewData["NewProducts"] = hNPro.AllNewsProductBySearch(pageNum, Config.ITEMS_PER_PAGE, null, Request.QueryString["text"].ToString());
                    var lstPro = hNPro.getHotNews(null,null,1, 5);
                    ViewData["HotProducts"] = lstPro;
                    ViewData["CurrentCategory"] = hCate.getCate(0);
                    ViewData["CurrentPage"] = page;
                    ViewData["NeedSell"] = needSell;
                    ViewData["ProvinceId"] = provinceId;
                    //}
                }
                return View(string.Format("~/Views/Home/{0}_List.aspx", page.Template.TemFilePath));
            }
            return Index();
        }


        //public ActionResult Detail(string productTitle)
        //{
        //    int productId;
        //    if (int.TryParse(Request.QueryString["prid"], out productId))
        //    {
        //        if (productId <= 0)
        //            return Index();
        //        loadCommon();
        //        NewsProductHelper hNews = new NewsProductHelper();
        //        NewsProduct news = hNews.getNews(productId);
        //        if (news != null)
        //        {
        //            CategoryHelper hCate = new CategoryHelper();
        //            PageType page = (news.Category.PageType != null ? news.Category.PageType : news.Category.ParentCategory.PageType);
        //            List<Category> lstCate = hCate.getCates(page.PageId);
        //            ViewData["Categories"] = lstCate;
        //            ViewData["CurrentCategory"] = hCate.getCate(news.CateId);
        //            ViewData["CurrentPage"] = page;
        //            ViewData["News"] = news;
        //            return View(string.Format("~/Views/Home/{0}_Detail.aspx", page.Template.TemFilePath));
        //        }
        //    }
        //    return Index();
        //}
    }
}
